package com.qqserver.service;

import com.qqcommon.Message;
import com.qqcommon.MessageType;
import com.qqcommon.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

/**
 *
 * 这是服务器,在监听9999端口,等待客户端的连接,并保持通信
 */

public class QQServer {

    private ServerSocket serverSocket=null;
    private static HashMap<String,User> validUsers=new HashMap<>();

    static {//添加一些用户
        validUsers.put("李星云",new User("李星云","123456"));
        validUsers.put("姬如雪",new User("姬如雪","123456"));
        validUsers.put("张子凡",new User("张子凡","123456"));
        validUsers.put("陆林轩",new User("陆林轩","123456"));
        validUsers.put("蚩梦",new User("蚩梦","123456"));
        validUsers.put("尤川",new User("尤川","123456"));
    }

    //验证用户是否有效的方法
    public static boolean checkUser(String userId,String pwd){
        User user=validUsers.get(userId);
        if(user.getUserid()==null){//判断用户是否存在
            return false;
        }
        if(!user.getPassword().equals(pwd)){//判断密码是否正确
            return false;
        }
        return true;
    }

    //构造器
    public QQServer() {
        try {
            System.out.println("服务器,在监听9999端口.....");
            serverSocket=new ServerSocket(9999);
            while(true){//当和某个客户端连接时会继续监听,故用while
                Socket socket=serverSocket.accept();
                //得到socket关联的对象输入流
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                //得到socket关联的对象输出流
                ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                //读取客户端发送的user对象
                User user=(User) ois.readObject();
                //创建一个Message对象准备回复客户端
                Message message=new Message();
                //验证checkUser(user.getUserid(), user.getPassword())
                if(checkUser(user.getUserid(), user.getPassword())){//登录成功
                    message.setMesType(MessageType.MESSAGE_LOGIN_SUCCEED);
                    oos.writeObject(message);
                    //创建一个线程,和客户端保持通信,该线程有Socket对象
                    ServerConnectClientThread serverConnectClientThread=new ServerConnectClientThread(socket, user.getUserid());
                    //启动该线程
                    serverConnectClientThread.start();
                    //把线程放入到一个集合中
                    ManagerClientThreads.addServerConnectClientThread(user.getUserid(), serverConnectClientThread);


                }else{//登录失败
                    System.out.println("用户名为"+user.getUserid()+"密码为"+user.getPassword()+"的用户验证失败了");
                    message.setMesType(MessageType.MESSAGE_LOGIN_FAIL);
                    oos.writeObject(message);
                    //关闭socket
                    socket.close();
                }

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {//如果服务器退出了循环,说明服务器不在监听,此时关闭ServerSocket
            try {
                serverSocket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


    }
}
